package category

import (
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/admin"
)

type Admin struct {

}

func (_ Admin) Setup() {
	Core.SetRoute("GET:/admin/categories/add", "[I]category.Admin.AddPageAction")
	Core.SetRoute("POST:/admin/categories/add", "[I]category.Admin.AddPostAction")
	Core.SetRoute("GET:/admin/categories/edit", "[I]category.Admin.EditPageAction")
	Core.SetRoute("POST:/admin/categories/edit", "[I]category.Admin.EditPostAction")
	Core.SetRoute("DELETE:/admin/categories/delete", "[I]category.Admin.DeleteAction")
	Core.SetRoute("GET:/admin/categories", "[I]category.Admin.ListPageAction")
}

// category list page
func (_ Admin) ListPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := make(map[string]interface {})
	data["Categories"] = GetList()
	response.Body = admin.RenderAdminPage("category/list.html", data, nil, "category")
}

// add category page
func (_ Admin) AddPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	response.Body = admin.RenderAdminPage("category/add.html", nil, nil, "category")
}

// post category page
func (_ Admin) AddPostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := request.Input().All()
	msg := validateCategoryData(data, "")
	res := map[string]interface {}{"res":false}
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	AddCategory(data)
	res["res"] = true
	response.Json(res)
	return
}

// edit category page
func (_ Admin) EditPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Redirect("/admin/categories/")
		return
	}
	id := request.Param[4]
	data := map[string]interface {}{}
	data["Category"] = GetOneById(id)
	response.Body = admin.RenderAdminPage("category/edit.html", data, nil, "category")
}

// edit post category action
func (_ Admin) EditPostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	data := request.Input().All()
	msg := validateCategoryData(data, id)
	res := map[string]interface {}{"res":false}
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	UpdateCategory(id, data)
	res["res"] = true
	response.Json(res)
	return
}

// delete category action
func (_ Admin) DeleteAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if len(request.Param) < 6 {
		response.Status = 400
		return
	}
	id := request.Param[4]
	res := map[string]interface {}{"res":false}
	DeleteCategory(id)
	res["res"] = true
	response.Json(res)
	return
}
